package com.baseframe.sms.controller;

import com.baseframe.sms.builds.SmsBuilds;


import com.baseframe.sms.constant.BaseSmsSendConstant;
import com.baseframe.sms.entity.Sms;
import com.baseframe.sms.entity.SmsSendData;
import com.baseframe.sms.sign.SmsPattern;
import com.baseframe.sms.sign.SmsScene;
import com.baseframe.sms.sign.SmsType;
import com.baseframe.sms.template.SmsTemplate;
import com.baseframe.sms.utils.SmsUtil;
import com.baseframe.sms.utils.VariableUtil;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.redis.CommonRedisUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sms/send")
public class SmsSendController {

    /**
     * 验证码短信
     * @param phone 手机号码
     * @return 发送结果
     */
    @GetMapping("/captcha")
    public R<Object> sendCaptcha(String phone){

        Sms sms = new Sms();
        sms.setType(SmsType.TENCENT);
        sms.setScene(SmsScene.CAPTCHA);
        sms.setAccessKey("AKIDMZYCcolU1JOFrENHJ7h5E9pLiw9FD2jI");
        sms.setSecretKey("U6av571nQsNp8GDxeAUaR6240mRuAD6q");
        sms.setAppId("1400834257");
        sms.setSign("舞指科技");
        sms.setTemplate("1351858");
        sms.setTemplateContent("{1}，5分钟内有效，仅可用于账户身份验证。为了保障您的账户安全，请勿向他人泄露验证码信息。 ");

        SmsTemplate template = SmsBuilds.getTemplate(sms);
        //获取随机验证码
        String code = VariableUtil.randomCode(6);

        Map<String, String> params = SmsUtil.buildParams(sms.getTemplateContent(), SmsPattern.TENCENT.getPattern(), code);
        SmsSendData sendData = new SmsSendData();
        sendData.setPhone(phone);
        sendData.setParams(params);
        R<Object> send = template.send(sendData);

        if(send.getCode() == 200){
            CommonRedisUtil.setValue(BaseSmsSendConstant.SMS_CAPTCHA_REDIS_KEY+phone,code,5L, TimeUnit.MINUTES);
        }

        return send;
    }

    /**
     * 通知短信
     * @param phone 手机号码
     * @return 发送结果
     */
    @GetMapping("/notice")
    public R<Object> sendNotice(String phone){

        return null;
    }

    /**
     * 营销短信
     * @param phone 手机号码
     * @return 发送结果
     */
    @GetMapping("/marketing")
    public R<Object> sendMarketing(String phone){

        return null;
    }
}
